#include<iostream>
using namespace std;
template <typename ttype>
class sector
{
	public:
		ttype value;
		sector *next;
};
template <typename ttype>
class myArray
{
	private:
		int array_length;
		sector<ttype> *head;
	public:
		myArray()//constructor
		{
			array_length=0;
			head=NULL;
		}
		void add(ttype add_val)//add a sector at the end;
		{
			if(head!=NULL)
			{
				sector<ttype> *ptr_sector=this->head;
				while(ptr_sector->next!=NULL)
					ptr_sector=ptr_sector->next;
				ptr_sector->next=new sector<ttype>;
				ptr_sector=ptr_sector->next;
				ptr_sector->next=NULL;
				ptr_sector->value=add_val;
			}
			else
			{
				this->head=new sector<ttype>;
				head->value=add_val;
				head->next=NULL;
			}
			this->array_length+=1;
		}
		void add(ttype add_value,int num)//add a sector after the no.num sector.the first sector's no is 1
		{
			if(num==0)
			{
				sector<ttype> *temporary=this->head;
				this->head=new sector<ttype>;
				this->head->next=temporary;
				this->head->value=add_value;
			}
			if(num>this->array_length)
			{
				cout<<"//error:adding after a non-exist sector"<<endl;
				return;
			}
			sector<ttype> *ptr_sector=this->head;
			for(int i=0;i<num-1;i++)
			{
				ptr_sector=ptr_sector->next;
			}
			sector<ttype> *temporary=ptr_sector->next;
			ptr_sector->next=new sector<ttype>;
			ptr_sector=ptr_sector->next;
			ptr_sector->next=temporary;
			ptr_sector->value=add_value;
			this->array_length+=1;
		}
		void erase(int num)//delete the no.num sector,the first sector is no.1
		{
			if(num<=0||num>this->array_length)
			{
				cout<<"//error:erasing an non-exist member"<<endl;
				return;
			}
			if(num==1)
			{
				sector<ttype> *temporary=this->head->next;
				delete this->head;
				this->head=temporary;
				this->array_length-=1;
			}
			else
			{
				sector<ttype> *ptr_sector=this->head;
				for(int i=0;i<num-2;i++)//the ptr_sector should point to the sector which is one 
				{						//place ahead of the one that needs to be deleted
					ptr_sector=ptr_sector->next;
				}
				sector<ttype> *temporary=ptr_sector->next->next;
				delete ptr_sector->next;
				ptr_sector->next=temporary;
				this->array_length-=1;
			}
		}
		int length()//return the length of the array;
		{
			return this->array_length;
		}
		void show()//show the whole array;
		{
			sector<ttype> *ptr_sector=this->head;
			int i=0;
			while(ptr_sector!=NULL)
			{
				i++;
				cout<<"//no."<<i<<":"<<ptr_sector->value<<endl;
				ptr_sector=ptr_sector->next;
			}
			cout<<"//end of the array"<<endl;
		}
		void des(sector<ttype> *del)//to destruct the whole array member after this one
		{
			sector<ttype> *temporary=del->next;
			if(temporary!=NULL)
			{
				this->des(temporary);
			}
			delete del;
		}
		~myArray()//destructor
		{
			if(this->head==NULL)
			{
				cout<<"//deconstruction completed"<<endl;
				return;
			}
			des(this->head);
			cout<<"//deconstruction completed"<<endl;
		}
};
int main()
{
	myArray<int> a;
	bool exit_flag=1;
	int command;
	while(exit_flag)
	{
		cout<<"//command 1:add at the end 2:add after an exact position \n//3:delete a member 4:show 0:exit"<<endl;
		cin>>command;
		if(command>4||command<0)
		{
			cout<<"//not a valid command"<<endl;
			continue;
		}
		switch(command)
		{
			case 1:
				cout<<"//enter the number"<<endl;
				int input;
				cin>>input;
				a.add(input);
				break;
			case 2:
				cout<<"//enter the number and then the position"<<endl;
				int input1,pos;
				cin>>input1>>pos;
				a.add(input1,pos);
				break;
			case 3:
				cout<<"//enter the position of the member you want to delete"<<endl;
				int pos1;
				cin>>pos1;
				a.erase(pos1);
				break;
			case 4:
				a.show();
				break;
			case 0:
				exit_flag=0;
				break;
		}
	}
	return 0;
}
